package com.sc.nft.dao;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.EcologyPoolSource;
import com.sc.nft.enums.EcologyPoolSourceStatusEnum;
import com.sc.nft.mapper.EcologyPoolSourceMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

/**
 * @author 刘国航
 * @since 2024/7/15
 */
@Slf4j
@Repository
public class EcologyPoolSourceDao extends ServiceImpl<EcologyPoolSourceMapper, EcologyPoolSource> {

    public Page<EcologyPoolSource> sourcePage(Page<EcologyPoolSource> page, Long userId, String collectionName, EcologyPoolSourceStatusEnum status, Date startTime, Date endTime) {
        LambdaQueryWrapper<EcologyPoolSource> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Objects.nonNull(userId), EcologyPoolSource::getUserId, userId);
        queryWrapper.like(StrUtil.isNotBlank(collectionName), EcologyPoolSource::getCollectionName, collectionName);
        queryWrapper.eq(Objects.nonNull(status), EcologyPoolSource::getStatus, status);
        queryWrapper.ge(Objects.nonNull(startTime), EcologyPoolSource::getStartTime, startTime);
        queryWrapper.le(Objects.nonNull(endTime), EcologyPoolSource::getStartTime, endTime);
        queryWrapper.orderByDesc(EcologyPoolSource::getStartTime);
        return page(page, queryWrapper);
    }

    /**
     * 唯一标识
     * @param pkCode
     * @return
     */
    public EcologyPoolSource getPkCode(String pkCode) {
        LambdaQueryWrapper<EcologyPoolSource> wrapper = Wrappers.lambdaQuery(EcologyPoolSource.class);
        wrapper.eq(EcologyPoolSource::getPkCode, pkCode);
        wrapper.orderByDesc(EcologyPoolSource::getId);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

    public void released(String pkCode, BigDecimal linkCount) {
        UpdateWrapper<EcologyPoolSource> wrapper = Wrappers.update();
        wrapper.eq("pk_code", pkCode);
        wrapper.setSql("released_link_day = released_link_day + 1");
        wrapper.setSql("wait_release_link_day = wait_release_link_day - 1");
        wrapper.setSql("released_link_count = released_link_count + " + linkCount);
        wrapper.setSql("wait_release_link_count = wait_release_link_count - " + linkCount);
        update(new EcologyPoolSource(), wrapper);
    }

    public void startReleasingPoints() {
        UpdateWrapper<EcologyPoolSource> wrapper = Wrappers.update();
        wrapper.lt("DATE(start_time)", DateUtil.date().toString("yyyy-MM-dd"));
        wrapper.eq("status",EcologyPoolSourceStatusEnum.NOT_START);
        wrapper.set("status",EcologyPoolSourceStatusEnum.HAVE_IN_HAND);
        update(new EcologyPoolSource(),wrapper);
    }

    public void finishReleasingPoints() {
        UpdateWrapper<EcologyPoolSource> wrapper = Wrappers.update();
        wrapper.lt("DATE(end_time)", DateUtil.date().toString("yyyy-MM-dd"));
        wrapper.le("wait_release_link_day",0);
        wrapper.eq("status",EcologyPoolSourceStatusEnum.HAVE_IN_HAND);
        wrapper.set("status",EcologyPoolSourceStatusEnum.END);
        update(new EcologyPoolSource(),wrapper);
    }
}
